package com.sx.manage.controller.consumer.center;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sx.common.util.NumberUtil;
import com.sx.manage.biz.consumer.system.IMSystemConsumerBiz;
import com.sx.manage.biz.relation.IMRelationConfigBiz;
import com.sx.manage.controller.base.MBaseController;
import com.sx.manage.servlet.ViewAndModel;
import com.sx.model.manage.mybatis.form.consumer.MSystemConsumerInfoForm;
import com.sx.model.manage.mybatis.vo.consumer.MSystemConsumerInfoVO;
import com.sx.model.manage.mybatis.vo.relation.MRelationConfigVO;
import com.sx.model.manage.mybatis.vo.result.ResultBooleanVO;
import com.sx.model.manage.mybatis.vo.result.ResultListVO;
import com.sx.model.manage.mybatis.vo.result.ResultObjectVO;

/**
 * 系统用户管理 控制层
 * @author zhenzhanlong  systemUser
 *
 */
@Controller
@RequestMapping("/systemConsumer")
public class MConsumerCenterManagerController extends MBaseController{
	private Logger log = LoggerFactory.getLogger(this.getClass());
	
	@Resource(name="systemConsumerBizImpl")
	private IMSystemConsumerBiz systemConsumerBizImpl;
	
	@Resource(name = "mRelationConfigBizImpl")
	public IMRelationConfigBiz mRelationConfigBizImpl;
	/**
	   * 跳转到 系统用户列表界面
	   */
	  @RequestMapping(value="sysConsumerIndex")
	  public ViewAndModel sysConsumerIndex() {
		  ViewAndModel modelView=new ViewAndModel(request,cache);
		  				  modelView.setViewName("consumer/system/systemConsumer");
		  /// 界面展开 菜单栏使用
		  modelView.addObject("current", "sysUser");				  
		  return modelView;
	  }

	/**
	 * 增加用户信息成功
	 */
	@RequestMapping(value="addConsumer")
	@ResponseBody
	public ResultBooleanVO addConsumer(MSystemConsumerInfoForm form){
		log.info("添加系统用户信息form={}",form);
		return systemConsumerBizImpl.add(form, loginConsumer());
	}
	/**更新用户信息*/
	@RequestMapping(value="updateConsumer")
	@ResponseBody
	public ResultBooleanVO  updateConsumer(MSystemConsumerInfoForm form){
		log.info("更新系统用户信息form={}",form);
		return systemConsumerBizImpl.update(form, loginConsumer());
	}
	/**更新用户信息*/
	@RequestMapping(value="saveConsumer")
	@ResponseBody
	public ResultBooleanVO  saveConsumer(MSystemConsumerInfoForm form){
		if(  null == form.getId()){
			log.info("添加系统用户信息form={}",form);
			return systemConsumerBizImpl.add(form,loginConsumer());
		}else{
			log.info("更新系统用户信息form={}",form);
			return systemConsumerBizImpl.update(form, loginConsumer());
		}
		
	}
	/**根据id 获取单个用户信息*/
	@RequestMapping(value="consumerById")
	@ResponseBody
	public  ResultObjectVO<MSystemConsumerInfoVO> consumerById(Long id){
		log.info("根据id查询系统用户信息id={}",id);
		return systemConsumerBizImpl.queryById(id);
	}
	/**根据 用户名，密码 获取个人用户信息*/
	@RequestMapping(value="consumerByCodeAndPassword")
	@ResponseBody
	public ResultObjectVO<MSystemConsumerInfoVO> consumerByCodeAndPassword(String consumer_Code,String password){
		log.info("根据 用户名，密码 获取系统用户信息consumer_Code={},password={}",consumer_Code,password);
		return systemConsumerBizImpl.queryByCodeAndPassword(consumer_Code, password);
	}
	/**获取用户信息列表*/
	@RequestMapping(value="consumerList")
	@ResponseBody
	public ResultListVO<MSystemConsumerInfoVO> consumerList(MSystemConsumerInfoForm form){
		log.info("查询系统用户集合form={}",form);
		return systemConsumerBizImpl.queryList(form);
	}
	/**获取用户分页信息*/
	@RequestMapping(value="consumerPage")
	@ResponseBody
	public ResultListVO<MSystemConsumerInfoVO> consumerPage(MSystemConsumerInfoForm form){
		log.info("查询系统用户列表form={}",form);
		return systemConsumerBizImpl.queryPage(form);
	}
	
	/***启用用户信息**/
	@RequestMapping(value="consumersEnable")
	@ResponseBody
	public ResultBooleanVO consumersEnable(Long[] consumer_ids){
		log.info("启用系统用户集合consumer_ids={}",NumberUtil.arrayToStr(consumer_ids));
		return systemConsumerBizImpl.updateStatusEnable(consumer_ids, loginConsumer());
	}
	/***禁用用户信息**/
	@RequestMapping(value="consumersDisable")
	@ResponseBody
	public ResultBooleanVO consumersDisable(Long[] consumer_ids){
		log.info("禁用系统用户集合consumer_ids={}",NumberUtil.arrayToStr(consumer_ids));
		return systemConsumerBizImpl.updateStatusDisable(consumer_ids, loginConsumer());
	}
	/***用户编码唯一性验证**/
	@RequestMapping(value="consumerCodeUniqueValidate")
	@ResponseBody
	public ResultBooleanVO consumerCodeUniqueValidate(String consumer_ode){
		log.info("验证用户编码是否唯一consumer_ode={}",consumer_ode);
		return systemConsumerBizImpl.uniqueCodeValidate(consumer_ode); 
	}
	/***删除系统用户**/
	@RequestMapping(value="deleteSystemConsumer")
	@ResponseBody
	public ResultBooleanVO deleteSystemConsumer(Long[] consumer_ids){
		log.info("删除系统用户集合consumer_ids={}",NumberUtil.arrayToStr(consumer_ids));
		ResultBooleanVO result =  systemConsumerBizImpl.deleteBatchByIds(consumer_ids);
		if(!result.isSuccess()) {
			return result;
		}
		// 系统用户角色配置
		ResultListVO<MRelationConfigVO> resultRela = this.mRelationConfigBizImpl.saveForSysConsumer(consumer_ids[0], new String[]{}, loginConsumer());
		if(!resultRela.isSuccess()) {
			return new ResultBooleanVO(false,null,resultRela.getMsg());
		}
		return new ResultBooleanVO();
	}

	/**
	 * 新编-系统用户密码修改
	 * 编辑系统用户密码
	 * @return
	 */
	@RequestMapping(value="editConsumerPassWord")
	@ResponseBody
	public ResultBooleanVO editConsumerPassWord(HttpServletRequest request, HttpServletResponse response)throws IOException{
		 if(null == request){
			 log.error(" editConsumerPassWord  request 为空");
			 return new ResultBooleanVO(false,null,"添加错误");
			}
		 String requestMessage = extractParamFromRequest(request);
		 if(null == requestMessage || "".equals(requestMessage)){
			 log.error(" editConsumerPassWord  requestMessage 为空");
			 return new ResultBooleanVO(false,null,"添加错误");
		}
		return systemConsumerBizImpl.editPassword(requestMessage);
	}
	
	
}
